package com.ffmpeg.demo;

import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ConvertUtils {
    @Test
    public void convert() throws IOException, InterruptedException {
        URL url = ConvertUtils.class.getClassLoader().getResource("ffmpeg/");
        System.out.println(url.getPath());
        //Runtime.getRuntime().exec(url.getPath()+" -i F:/input/11213.mp3  -ac 1 F:/output/test123.wmv");
        List<String> command = new ArrayList<String>();
        command.add(url.getPath() + "ffmpeg");
        command.add("-i");
        command.add("F:\\input\\11213.mp3");
        command.add("-ar");//音频采样率
        command.add("8000");
        command.add("-ac");//设置声道
        command.add("1");
        command.add("F:\\output\\test02.wav");
        Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
        //new PrintStream2(videoProcess.getErrorStream()).start();
        new PrintStream(videoProcess.getInputStream()).start();
        videoProcess.waitFor();

    }

    public static void main(String[] args) throws IOException {
        //convert();
    }

    class PrintStream extends Thread {
        java.io.InputStream __is = null;

        public PrintStream(java.io.InputStream is) {
            __is = is;
        }

        public void run() {
            try {
                while (this != null) {

                    int _ch = __is.read();
                    if (_ch != -1)
                        System.out.print((char) _ch);
                    else break;
                }
                System.out.println("转换结束");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    class PrintStream2 extends Thread {
        java.io.InputStream __is = null;

        public PrintStream2(java.io.InputStream is) {
            __is = is;
        }

        public void run() {
            try {
                while (this != null) {

                    int _ch = __is.read();
                    if (_ch != -1)
                        System.out.print((char) _ch);
                    else break;
                }
                System.out.println("转换失败");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
